package com.yhh.goods.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhh.common.base.ret.ApiResult;
import com.yhh.common.entity.goods.dto.ProductDto;
import com.yhh.common.entity.goods.model.Product;
import com.yhh.common.utils.FieldsFilter;
import com.yhh.goods.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  商品信息服务API
 * </p>
 *
 * @author yhh
 * @since 2021-04-21
 */
@RestController
@RequestMapping("goods/product/api/v1")
@Api(tags = "商品信息服务API")
@Slf4j
public class ProductController {

    @Autowired
    private IProductService productService;

    @ApiOperation(value = "查询商品信息")
    @GetMapping("getProducts")
    public ApiResult getProducts(ProductDto dto){
        if(dto == null)
            return ApiResult.error("没有查询到您需要的信息");
        if(dto.getPageNum() == null || dto.getPageSize() == null){
            return ApiResult.error("请传入分页参数");
        }
        IPage page = new Page(dto.getPageNum(), dto.getPageSize());
        QueryWrapper wrapper = new QueryWrapper();
        if(dto.getName() != null){
            wrapper.like("name",dto.getName());
        }
        if(dto.getMinPrice() !=null){
            wrapper.ge("price",dto.getMinPrice());
        }
        if(dto.getMaxPrice() !=null){
            wrapper.le("price",dto.getMaxPrice());
        }
        wrapper.eq("status",dto.getStatus());

        page = productService.page(page,wrapper);
        List records = page.getRecords();
        return ApiResult.success().put("products",page.setRecords(FieldsFilter.filter(records,
                Arrays.asList("id","name","price","mainImg"))));
    }

    @ApiOperation(value = "根据ID查询商品信息")
    @GetMapping("getProductById")
    public ApiResult getProductById(Integer id){
        return ApiResult.success().put("product",productService.getById(id));
    }

    @ApiOperation(value = "添加商品信息")
    @PostMapping("addProduct")
    public ApiResult addProduct(@Validated({Product.Save.class}) @RequestBody Product product){
        product.setCreateTime(new Date());
        product.setUpdateTime(new Date());
        product.setStatus(1);
        if(productService.save(product)){
            return ApiResult.success();
        }
        return ApiResult.error("添加商品信息失败");
    }

    @ApiOperation(value = "修改商品信息")
    @PostMapping("updateProduct")
    public ApiResult updateProduct(@Validated({Product.Update.class}) @RequestBody Product product){
        product.setUpdateTime(new Date());
        if(productService.updateById(product)){
            return ApiResult.success();
        }
        return ApiResult.error("添加商品信息失败");
    }

    @ApiOperation(value = "删除商品信息")
    @PostMapping("delProduct")
    public ApiResult delProduct(Integer id){
        if(productService.removeById(id)){
            return ApiResult.success();
        }
        return ApiResult.error("删除商品信息失败");
    }
}
